package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.GoodsMapper;
import com.gxa.zhonglong.mapper.GoodsPictureMapper;
import com.gxa.zhonglong.pojo.Goods;
import com.gxa.zhonglong.pojo.GoodsPicture;
import com.gxa.zhonglong.pojo.Where;
import com.gxa.zhonglong.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@Service
public class GoodsServiceImp implements GoodsService {

    @Autowired
    GoodsPictureMapper goodsPictureMapper = null;

    @Autowired
    GoodsMapper goodsMapper = null;

    @Override
    public void insertGoods(Goods goods) {

        goodsMapper.insertGoods(goods);

    }

    @Override
    public void deleteByNumber(String number) throws BusinessException {
        List<Map<String, Goods>> maps = goodsMapper.selectByNumber(number);
        if (maps.size()>0){
            goodsMapper.deleteByNumber(number);
        }else {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }

    }

    @Override
    public List<Map<String,Object>> selectByNumberAllFiled(String number) {
        List<Map<String,Object>> list= goodsMapper.selectByNumberAllFiled(number);
        return list;
    }

    @Override
    public List<Map<String, Goods>> selectByNumber(String number) {
        List<Map<String, Goods>> maps = goodsMapper.selectByNumber(number);
        return maps;
    }


    @Override
    public List<Map<String, Object>> selectAll(Where where) {
        Integer page = where.getPage();
        Integer size = where.getSize();
        where.setPage((page-1)*size);
        List<Map<String, Object>> maps = goodsMapper.selectAll(where);
        return  maps;
    }

    @Override
    public Goods selectGoods(String number) {
        Goods goods = goodsMapper.selectGoods(number);
        return goods;
    }

    @Override
    public List<Map<String, Object>> selectAllNoPage(Where where) {
        where.setPage(null);
        List<Map<String, Object>> maps = goodsMapper.selectAll(where);
        return maps;
    }

    @Override
    public Integer selectAllCount() {
        Integer count = goodsMapper.selectAllCount();
        return  count;
    }
}
